/**
 * 
 */
package com.suneee.category.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.category.dao.CategoryDao;
import com.suneee.category.entity.Category;
import com.suneee.context.page.Sort;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;

/**
 * @author liuxh 类目管理dao层
 */
@Repository("categoryDao")
public class CategoryDaoImpl extends GenericDao<Category, String>implements CategoryDao {

	@Override
	public List<Map<String, Object>> getChildrenCodeListByParentCode(String parentCode) {
		String result = (parentCode == null ? "" : parentCode.trim());
		Object[] args = new Object[1];
		args[0] = result + "__";
		List<Map<String, Object>> list = this.queryForList(
				"select category_code from t_category where category_code like ? order by category_code ", args);
		return list;
	}

	@Override
	public boolean existsChildrenName(String name, String parentCode) {
		Object[] args = new Object[2];
		args[0] = parentCode + "__";
		args[1] = name;
		List<Category> list = this.selectWhere(" category_code like ? and name = ? ", args);
		return list != null && list.size() > 0;
	}

	@Override
	public boolean exsitsChildNode(String parentId) {
		String result = (parentId == null ? "" : parentId.trim());
		Object[] args = new Object[2];
		args[0] = result + "%";
		args[1] = result.length() + 2;
		List<Category> list = this.selectWhere(" category_code like ? and length(category_code) = ? ", args);
		return list != null && list.size() > 0;
	}

	@Override
	public List<Category> listAll() {
		return selectWhere(" 1=1 ", new Sort(" category_code asc , order_number asc "));
	}

	@Override
	public List<Category> findCategory(String name) {
		/*Object[] args = new Object[1];
		args[0] = "%" +name + "%";
		return selectWhere(" name like ? ", new Sort(" category_code asc , order_number asc "), args);*/
		List<Category> resultList = new ArrayList<Category>();
		Object[] args = null;
		if(null != name && !"".equals(name))
		{
			args = new Object[1];
			args[0] = "%" +name + "%";
			List<Map<String,Object>> mapList = this.queryForList("SELECT DISTINCT SUBSTR(category_code,1,2) as category_code FROM t_category WHERE NAME LIKE ? ", args);
			WhereClauseBuilder build = null;
			for(Map<String,Object> map: mapList)
			{
				if(null != map && !"".equals(map.get("category_code")))
				{
					build = new WhereClauseBuilder();
					build.startsWith("category_code", map.get("category_code").toString());
					List<Category> catList = this.selectWhere(build.getSql(), build.getArgs());
					if(null != catList && catList.size() > 0)
					{
						resultList.addAll(catList);
					}
				}
			}
			if(resultList.size() > 0)
			{
				return resultList;
			}
			return null;
		}
		return selectWhere(" 1 = 1 ", new Sort(" category_code asc , order_number asc "), args);
	}
}
